<?php

namespace app\appapi\controller;


use app\appapi\controller\BaseController;
use app\appapi\service\TrainService;
use app\data\model\LiveUserList;
use app\data\model\LiveUserLoginlog;

class User extends BaseController
{
    //获取用户信息（注销）
    public function getInfo()
    {
        $userId = $this->checkTokenReturnUserId();
        $user = LiveUserList::mk()->field('user_name')->where(['id' => $userId, 'deleted' => 0])->find();
        $this->success('查询成功！', $user);
    }
    //检测用户信息
    public function checkUserInfo()
    {
        /**
         * -500：无token参数：认证异常
         * -400：登录认证无效（没有找到token）：其他设备登录
         * -300：登录认证失效（token到期）
         * -200：禁止登录
         * -100：会员已到期
         */
        $userId = $this->checkTokenReturnUserId();
        $user = LiveUserList::mk()->where(['id' => $userId, 'deleted' => 0])->find();
        if($user['login_versionCode'] < 7) $this->error('请在使用文档中更新到最新版本！低版本暂不支持登录！','',-200);
        if (empty($user)) $this->error('不存在该账户！');
        if ($user['user_state'] == 1) $this->error('当前用户禁止登录！','',-200);
        if ($user['thali_endtime'] < date('Y-m-d H:i:s')) $this->error('会员已到期，请联系客服！','',-100);
        if (LiveUserLoginlog::mk()->where(['user_id' => $user['id']])->whereBetween('login_time', [date('Y-m-d') . ' 00:00:00', date('Y-m-d') . ' 23:59:59'])->count() == 0) {
            LiveUserLoginlog::mk()->insert(['user_id' => $user['id'], 'app_id' => $user['app_id'], 'login_time' => date('Y-m-d H:i:s')]);
        }
        $this->success('检测成功！');
    }
    //修改密码
    public function updatePassword(){
        $post = $this->request->post();
        $userId = $this->checkTokenReturnUserId();
        if (!isset($post['new_user_password'], $post['old_user_password'])) $this->error('缺少参数');
        $user = LiveUserList::mk()->where(['id' => $userId])->find();
        if (empty($user)) $this->error('不存在该用户！');
        if ($user['user_password'] != $post['old_user_password']) $this->error('旧密码不正确！');

        //更改用户
        LiveUserList::mk()->where(['id' => $user['id']])->update([
            'user_password' => $post['new_user_password'],
        ]);
        $this->success('改密成功');
    }
    //修改昵称
    public function updateNickname(){
        $post = $this->request->post();
        $userId = $this->checkTokenReturnUserId();
        if (!isset($post['new_nickname'])) $this->error('缺少参数');
        $user = LiveUserList::mk()->where(['id' => $userId])->find();
        if (empty($user)) $this->error('不存在该用户！');

        //更改用户
        LiveUserList::mk()->where(['id' => $user['id']])->update([
            'user_nickname' => $post['new_nickname'],
        ]);
        $this->success('更改成功');
    }
    //获取我的邀请码
    public function getMyInvicode(){
        $userId = $this->checkTokenReturnUserId();
        $user = LiveUserList::mk()->where(['id' => $userId])->find();
        $this->success('获取成功',['invicode'=>$user['user_invicode']]);
    }
    //获取是否向量授权（注销）
    public function getQaStatus(){
        $userId = $this->checkTokenReturnUserId();
        $user = LiveUserList::mk()->where(['id' => $userId])->find();
        if($user['qa_status'] == 1){
            $data = [
                'qa_collection' =>$user['qa_collection'],
                'dash_scope_key'=>TrainService::DashScopeAPIKEY,
                'dash_vector_key'=>TrainService::DashVectorAPIKEY,
            ];
            $this->success('授权成功',$data);
        }else{
            $this->error('未授权，请联系客服');
        }
    }
    //获取是否AI大模型授权（注销）
    public function getTrainStatus(){
        $userId = $this->checkTokenReturnUserId();
        $user = LiveUserList::mk()->where(['id' => $userId])->find();
        if($user['train_status'] == 1){
            $data = [
                'train_collection'=>$user['train_collection'],
                'dash_scope_key'=>TrainService::DashScopeAPIKEY,
                'dash_vector_key'=>TrainService::DashVectorAPIKEY,
            ];
            $this->success('授权成功',$data);
        }else{
            $this->error('未授权，请联系客服');
        }
    }
}